package com.yrcm.eduapp.activity.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yrcm.eduapp.R;
import com.yrcm.eduapp.entity.QuestionEntity;

public class ZXDYTimuAdapter extends BaseAdapter {
	private List<QuestionEntity> data;
	private Context mContext;
	private LayoutInflater inflater;
	private int selectItem = 0;
	private int typeCount;

	public ZXDYTimuAdapter(Context context, List<QuestionEntity> list,
			int typeCount) {
		this.mContext = context;
		this.data = list;
		this.inflater = LayoutInflater.from(mContext);
		this.typeCount = typeCount;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return data.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return data.get(position).getType();
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return typeCount;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		HolderView holder;

		if (view == null) {
			holder = new HolderView();
			switch (getItemViewType(position)) {
			case 1:
				view = inflater.inflate(R.layout.zxdy_home_item1, null);
				holder.nameTxt = (TextView) view.findViewById(R.id.timu_txt);
				holder.timeTxt = (TextView) view.findViewById(R.id.shijian_txt);
				holder.statusImg = (ImageView) view
						.findViewById(R.id.img_status);

				break;
			case 0:
				view = inflater.inflate(R.layout.zxdy_home_item2, null);
				holder.nameTxt = (TextView) view.findViewById(R.id.tip_txt);

				break;
			}

		} else {
			holder = (HolderView) view.getTag();
		}

		switch (getItemViewType(position)) {
		case 0:
			holder.nameTxt.setText(data.get(position).getName());
			break;
		case 1:
			// System.out.println(data.get(position).getTitle() + "==========");
			if (data.get(position).getTitle() != null) {
				holder.nameTxt.setText(Html.fromHtml(data.get(position)
						.getTitle()));
			} else {
				holder.nameTxt.setText("");
			}
			if (data.get(position).isJd()) {
				holder.timeTxt.setText(data.get(position).getAnswerTime());
			} else {
				holder.timeTxt.setText(data.get(position).getCreateTime());
			}
			switch (data.get(position).getStatus()) {
			case 0:
				holder.statusImg
						.setBackgroundResource(R.drawable.status_hongse);
				break;
			case 1:
				holder.statusImg.setBackgroundResource(R.drawable.status_luse);
				break;
			case 2:
				holder.statusImg.setBackgroundResource(R.drawable.status_huise);
				break;
			default:
				holder.statusImg.setVisibility(View.GONE);
				break;
			}
		default:
			break;
		}

		if (position == selectItem) {
			view.setBackgroundColor(mContext.getResources().getColor(
					R.color.zxdy_timu_s));
		} else {
			view.setBackgroundColor(mContext.getResources().getColor(
					R.color.zxdy_timu));

		}

		view.setTag(holder);
		return view;
	}

	class HolderView {
		ImageView statusImg;
		TextView nameTxt;
		TextView timeTxt;
	}

	@Override
	public boolean isEnabled(int position) {

		switch (getItemViewType(position)) {
		case 0:
			return false;
		case 1:
			return true;
		}
		return false;
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}

}
